<template>
  <div>
    <el-card :body-style="{paddingBottom:0}">
      <el-form :model="formData" inline size="mini" label-width="70px">
        <el-form-item label="uuid">
          <el-input class="form-input" style="width: 200px" clearable placeholder="uuid" v-model="formData.uuid"
                    @change="getDeviceList"></el-input>
        </el-form-item>
        <el-form-item label="剩余次数">
          <el-input class="form-input" style="width: 200px" clearable placeholder="剩余次数" v-model="formData.use_count"
                    @change="getDeviceList"></el-input>
        </el-form-item>
        <el-form-item label="ip信息">
          <el-input class="form-input" style="width: 200px" clearable placeholder="ip信息" v-model="formData.ip_info"
                    @change="getDeviceList"></el-input>
        </el-form-item>
        <el-form-item label="设备信息">
          <el-input class="form-input" style="width: 200px" clearable placeholder="设备信息" v-model="formData.platform"
                    @change="getDeviceList"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker class="form-date-select" v-model="formData.dateTime" type="datetimerange"
                          start-placeholder="开始日期" end-placeholder="结束日期"
                          :default-time="['12:00:00']" @change="getDeviceList">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <div class="hor-layout-center">
            <div style="flex: 1"></div>
            <el-button type="primary" size="mini" @click="getDeviceList">搜索</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table :data="tableData" border fit stripe center highlight-current-row size="mini" style="width: 100%">
        <el-table-column prop="id" label="id" show-overflow-tooltip width="40"/>
        <el-table-column prop="uuid" label="uuid" show-overflow-tooltip width="190">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="toDetail(scope.row)">{{ scope.row.uuid }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="use_count" label="剩余次数" show-overflow-tooltip width="80">
          <template slot-scope="scope">
            <el-tag effect="light" type="success">{{ scope.row.use_count }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip_info" label="ip地址" show-overflow-tooltip width="160"/>
        <el-table-column prop="platform" label="设备信息" show-overflow-tooltip/>
        <el-table-column prop="create_time" label="创建时间" show-overflow-tooltip width="160"/>
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="updateDevice(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="hor-layout" style="margin-top: 10px">
        <div style="flex: 1"></div>
        <el-pagination class="table-pager" small background layout="prev, pager, next, total"
                       :page-size="queryParams.pageSize" :current-page.sync="queryParams.current"
                       :total="queryParams.total" @current-change="getDeviceList"/>
      </div>
    </el-card>
    <el-dialog title="修改设备信息" :visible.sync="dialogVisible" width="40%" @close="cancelUpdate">
      <el-card>
        <el-form size="small" label-width="auto">
          <el-form-item label="uuid">
            <el-input size="small" v-model="deviceObj.uuid" disabled class="input-view"></el-input>
          </el-form-item>
          <el-form-item label="ip地址">
            <el-input size="small" v-model="deviceObj.ip_info" disabled class="input-view"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input size="small" v-model="deviceObj.create_time" disabled class="input-view"></el-input>
          </el-form-item>
          <el-form-item label="剩余次数">
            <el-input size="small" v-model="deviceObj.use_count" clearable class="input-view"></el-input>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdate">取 消</el-button>
        <el-button type="primary" @click="confirmUpdate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deviceApi} from "../../api/device"
import {deviceDetailPage} from "@/router/pages.js"

export default {
  name: "index",
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      deviceObj: {},
      formData: {
        uuid: '',
        use_count: '',
        ip_info: '',
        platform: '',
        dateTime: '',
      },
      queryParams: {
        pageSize: 10,
        pageNum: 0,
        total: 0,
        current: 1
      }
    }
  },
  watch: {
    formData: {
      handler(newVal) {
        this.tableData = []
        this.queryParams.current = 1
        this.queryParams.total = 0
      },
      deep: true
    },
  },
  mounted() {
    this.getDeviceList()
  },
  methods: {
    getDeviceList() {
      let {current, pageSize} = this.queryParams
      let pageNum = pageSize * (current - 1)
      deviceApi.getDeviceList({...this.formData, ...this.queryParams, pageNum}).then(res => {
        this.tableData = res.data.list
        this.queryParams.total = res.data.total
      })
    },
    toDetail(item) {
      this.$router.push({path: deviceDetailPage, query: {...item}})
    },
    updateDevice(item) {
      this.deviceObj = {...item}
      this.dialogVisible = true
    },
    cancelUpdate() {
      this.deviceObj = {}
      this.dialogVisible = false
    },
    confirmUpdate() {
      deviceApi.updateDeviceInfo(this.deviceObj).then(res => {
        this.dialogVisible = false
        this.deviceObj = {}
        this.$message({message: res.data, type: res.code == 0 ? 'success' : 'error'})
        if (res.code == 0) this.getDeviceList()
      })
    }
  }
}
</script>

<style scoped>
.input-view {
  width: 200px;
}

.form-input {
  width: 120px;
}

.form-date-select {
  width: 320px;
}
</style>
